/*
 * This file is part of the Boring Engine's Kirby Game.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package kirby;

import com.googlecode.boringengine.Log;
import com.googlecode.boringengine.Render;
import com.googlecode.boringengine.lua.LuaFunction;

public class Camera {

    private static int x, y, levelw, levelh;
    private static GameObject target;
    private static int targetx, targety;

    public static void update() {
        int mx = x + 400, my = y + 278;
        int targetmx = 0, targetmy = 0;
        int dx = 0, dy = 0;
        if (target == null) {
            targetmx = targetx;
            targetmy = targety;
        } else {
            targetmx = target.getX() + target.getWidth() / 2;
            targetmy = target.getY() + target.getHeight() / 2;
        }
        if (mx != targetmx || my != targetmy) {
            dx += Math.sqrt(Math.abs(mx-targetmx)) * (targetmx - mx > 0 ? 1 : -1);
            dy += Math.sqrt(Math.abs(my-targetmy)) * (targetmy - my > 0 ? 1 : -1);
        }
        x += dx; y += dy;
        if (x + 800 > levelw) x = levelw - 800;
        if (y + 556 > levelh) y = levelh - 556;
        if (x < 0) x = 0;
        if (y < 0) y = 0;
    }

    public static void beginDraw() {
        Render.moveOverWithoutScale(-x, -y);
    }

    public static void endDraw() {
        //Render.moveOverWithoutScale(x, y);
        Render.setPosition(0, 0);
    }

    public static void follow(GameObject obj) {
        if (obj == null && target != null) {
            targetx = target.getX() + target.getWidth() / 2;
            targety = target.getY() + target.getHeight() / 2;
        }
        target = obj;
    }

    public static void follow(int x, int y) {
        target = null;
        targetx = x;
        targety = y;
    }

    public static void setLevel(Level level) {
        levelw = level.getWidth();
        levelh = level.getHeight();
    }

    public static int getBelowCamera() {
        return y + 556;
    }

    @LuaFunction
    public void followObj(Object obj) {
        if (obj instanceof GameObject)
            Camera.follow((GameObject) obj);
        else
            /*lua*/Log.error("follow(obj) called with a non-GameObject object");
    }

    @LuaFunction
    public void followXY(int luaX, int luaY) {
        Camera.follow(luaX, luaY);
    }
}
